﻿using Microsoft.WindowsAzure.Storage.Table;
using POC.Azure.Model;
using POC.Azure.Storage;
using POC.Loggers.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace POC.Loggers.Listeners
{
    public class TableStorageListener : AzureStorageTraceListener
    {
        Diagnostics _logger;
        string tableName;
        bool initStarted = false;
        public TableStorageListener(): base("async=true")
        {
        }
        public TableStorageListener(string initializerData)
            : base(initializerData)
        {
          
        }
 
        protected override void InitializeService()
        {
            base.Initialize();
            tableName = this.settings.GetRollingName("table") ?? this.settings.tableName;
            var repository = new TableStorageRespository<LogEntry>(this.storageAccount, tableName);
            _logger = new Diagnostics(repository);           
            repository.InitializeRepository();
        }
        protected override void PutEntry(LogEntry entry)
        { 
            if (null == _logger)
                this.InitializeService();
            _logger.PutEntry(entry, async);           
        }
        public override void Flush()
        {
            _logger.Flush(async);
            base.Flush();
        }
    }
}
